﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace PencatatInfoKebakaran
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitialUI();
                GridView_Main_Databind();
            }
        }

        #region UI
        protected void InitialUI()
        {
            MultiView_Main.SetActiveView(View_List);
        }

        protected void SetForm()
        {
            MultiView_Main.SetActiveView(View_Form);
        }
        #endregion UI

        #region GridView
        protected void GridView_Main_Databind()
        {
            KebakaranDBEntities db = new KebakaranDBEntities();
            var data = db.InfoKebakarans.Where(p => p.Status.Equals("AKTIF")).ToList<InfoKebakaran>();

            GridView_Main.DataSource = data;
            GridView_Main.DataBind();
        }

        protected void GridView_Main_SelectedIndexChanged(object sender, EventArgs e)
        {
            int id = Convert.ToInt32(GridView_Main.SelectedValue);

            KebakaranDBEntities db = new KebakaranDBEntities();
            InfoKebakaran item = db.InfoKebakarans.Where(p => p.KebakaranID.Equals(id)).Single<InfoKebakaran>();

            SetForm();

            TextBox_Lokasi.Text = item.LokasiKebakaran;
            TextBox_Longitude.Text = Convert.ToString(item.Longitude);
            TextBox_Latitude.Text = Convert.ToString(item.Latitude);
        }

        protected void GridView_Main_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Erase")
            {
                KebakaranDBEntities db = new KebakaranDBEntities();
                int id = Convert.ToInt32(e.CommandArgument);
                InfoKebakaran item = db.InfoKebakarans.Where(p => p.KebakaranID.Equals(id)).Single<InfoKebakaran>();
                db.InfoKebakarans.Remove(item);
                db.SaveChanges();

                GridView_Main_Databind();
                GridView_Main.SelectedIndex = -1;
                GridView_Main.PageIndex = 0;

                ExecClientFunction();
            }
        }
        #endregion GridView

        #region Action
        protected void ImageButton_Action_Add_Click(object sender, ImageClickEventArgs e)
        {
            SetForm();
        }

        protected void ImageButton_Action_Cancel_Click(object sender, ImageClickEventArgs e)
        {
            InitialUI();
        }

        protected void ImageButton_Action_Save_Click(object sender, ImageClickEventArgs e)
        {
            var db = new KebakaranDBEntities();
            InfoKebakaran item = new InfoKebakaran();
            bool isNewData = (GridView_Main.SelectedIndex == -1);

            if (!isNewData)
            {
                int id = Convert.ToInt32(GridView_Main.SelectedValue);
                item = db.InfoKebakarans.Where(p => p.KebakaranID.Equals(id)).Single<InfoKebakaran>();
            }

            item.LokasiKebakaran = TextBox_Lokasi.Text;
            item.WaktuInput = DateTime.Now;

            if (!String.IsNullOrEmpty(TextBox_Longitude.Text))
            {
                item.Longitude = Convert.ToDecimal(TextBox_Longitude.Text);
            }

            if (!String.IsNullOrEmpty(TextBox_Latitude.Text))
            {
                item.Latitude = Convert.ToDecimal(TextBox_Latitude.Text);
            }

            item.Status = "AKTIF";

            if (isNewData)
            {
                db.InfoKebakarans.Add(item);
            }
            db.SaveChanges();

            InitialUI();
            GridView_Main_Databind();

            ExecClientFunction();
        }
        #endregion Action

        #region Additional Method
        protected void ExecClientFunction()
        {
            ScriptManager.RegisterClientScriptBlock(this, GetType(), "", "sendToServer", true);
        }
        #endregion Additional Method
    }
}